import requests
import json

api_key = "ruYRm24ncaYfWgu7jmZH03L4"
secret_key = "XMhzFtXxnsUzSKL1670IAV2GDjlUURXk"

def get_access_token():
    url = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=%s&client_secret=%s"%(api_key, secret_key)
    payload = json.dumps("")
    headers = {
            'Content-Type': 'application/json',
            'Accept': 'application/json'
    }
    response = requests.request("POST", url, headers=headers, data=payload)
    return response.json().get("access_token")

def ask(content: str):
    url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/ernie_speed?access_token=" + get_access_token()
    payload = json.dumps({
        "messages": [
            {
                "role": "user",
                "content": content
            }
        ]
    })
    headers = {
        'Content-Type': 'application/json'
    }
    response = requests.request("POST", url, headers=headers, data=payload)
    print(response.text)

if __name__ == '__main__':
    ask("who are you ? please answer me in English")
